/*
 * Created by kopper on 13.02.15 16:53
 * (C) Copyright 2015 kopperek@gmail.com
 *
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the GNU Lesser General Public License
 * (LGPL) version 2.1 which accompanies this distribution, and is available at
 * http://www.gnu.org/licenses/lgpl-2.1.html
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 */

package app.android.kopper.foramemo;

import android.app.Activity;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.Toast;

import java.io.IOException;
import java.util.Calendar;

import app.android.kopper.foramemo.util.ExportUtil;
import app.android.kopper.foramemo.util.ForaMemoDbHelper;
import app.android.kopper.foramemo.util.LogProxy;

public class WasLazyActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_was_lazy);
        findViewById(R.id.button_ok).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                DatePicker calendar=(DatePicker)findViewById(R.id.calendar);
                Calendar c=Calendar.getInstance();
                c.set(calendar.getYear(),calendar.getMonth(),calendar.getDayOfMonth());
                trim(c);
                Calendar current=Calendar.getInstance();
                trim(current);
                if(current.compareTo(c)<0) {
                    Toast.makeText(getApplicationContext(),"Selected date can not be in the future",Toast.LENGTH_SHORT).show();
                    return;
                }
                EditText daysWidget=(EditText)findViewById(R.id.text_days);
                String daysText=daysWidget.getText().toString();
                if(daysText.length()==0) {
                    Toast.makeText(getApplicationContext(),"Number of days can not be empty",Toast.LENGTH_SHORT).show();
                    return;
                }
                int days=Integer.valueOf(daysText).intValue();
                LogProxy.i("postpone: "+c.getTime()+" / "+days);

                ForaMemoDbHelper dbHelper=new ForaMemoDbHelper(getApplicationContext());

                try {
                    ExportUtil.performExport(dbHelper,".backup");
                    SQLiteDatabase writableDatabase=dbHelper.getWritableDatabase();
                    writableDatabase.beginTransaction();
                    writableDatabase.execSQL("update "+ForaMemoDbHelper.FeedEntry.TABLE_NAME+" "+
                            "set "+ForaMemoDbHelper.FeedEntry.COLUMN_NAME_NEXT_OCCURRENCE+" = "+
                            " "+ForaMemoDbHelper.FeedEntry.COLUMN_NAME_NEXT_OCCURRENCE+" + "+days+"*1000*60*60*24  "+
                            " where "+ForaMemoDbHelper.FeedEntry.COLUMN_NAME_NEXT_OCCURRENCE+"  >= "+ForaMemoDbHelper.normalizeDate(c.getTime())+" ");
                    writableDatabase.setTransactionSuccessful();
                    writableDatabase.endTransaction();
                    Toast.makeText(getApplicationContext(),"Postponed",Toast.LENGTH_SHORT).show();
                    finish();
                } catch(IOException e) {
                    LogProxy.e("",e);
                    Toast.makeText(getApplicationContext(),e.getLocalizedMessage(),Toast.LENGTH_LONG).show();
                }
            }
        });
    }

    private void trim(Calendar c) {
        for(int field:new int[]{Calendar.HOUR,Calendar.MINUTE,Calendar.SECOND,Calendar.MILLISECOND})
            c.set(field,c.getMinimum(field));
    }
}
